<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="终端imei号" prop="termImei" width="30%">
        <el-input
          v-model.trim="queryParams.termImei"
          placeholder="请输入终端imei号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端类别" prop="termKind" width="30%">
        <el-select v-model="queryParams.termKind" placeholder="请选择终端类别" clearable size="small">
          <el-option
            v-for="dict in termKindOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paraList">
      <el-table-column label="终端imei号" align="center" prop="termImei" min-width="160%" />
      <el-table-column label="终端类别" align="center" prop="termKind" :formatter="termKindFormat" min-width="120%"/>
      <el-table-column label="绑定对象" align="center" prop="perBound" min-width="200%"/>
      <el-table-column label="历史位置记录表最大记录数" align="center" prop="hstRecMaxLen" min-width="200%"/>
      <el-table-column label="通讯服务器软件" align="center" prop="commServerId" min-width="200%"/>
      <el-table-column label="通讯服务器ip" align="center" prop="commServerIp" min-width="160%"/>
      <el-table-column label="通讯服务器端口" align="center" prop="commServerPort" min-width="160%"/>
      <el-table-column label="创建时间" align="center" prop="gmtCreate" min-width="160%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="gmtModified" min-width="160%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtModified, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="120%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['terminal:para:edit']"
          >设置参数</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="top">
        <el-form-item label="历史位置记录表最大记录数" prop="hstRecMaxLen" >
          <el-input v-model.trim="form.hstRecMaxLen" placeholder="请输入终端中保存的历史位置记录表的最大长度" />
        </el-form-item>
        <el-form-item label="通讯服务器软件" prop="commServerId">
          <el-input v-model.trim="form.commServerId" placeholder="请输入通讯服务器软件编码" />
        </el-form-item>
        <el-form-item label="通讯服务器ip" prop="commServerIp">
          <el-input v-model.trim="form.commServerIp" placeholder="请输入通讯服务器ip地址" />
        </el-form-item>
        <el-form-item label="通讯服务器端口" prop="commServerPort">
          <el-input v-model.trim="form.commServerPort" placeholder="请输入通讯服务器端口地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPara, getPara, updatePara } from "@/api/terminal/para";

export default {
  name: "Para",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数管理表格数据
      paraList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 终端类别字典
      termKindOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        termImei: null,
        termKind: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hstRecMaxLen: [
           { type: "number", max: 2147483647, message: "终端中保存的历史位置记录表的最大长度不能超过2147483647", trigger: "blur", transform: (value) => Number(value) }
        ],
        commServerId: [
           { max: 20, message: "通讯服务器软件编码不能超过20位", trigger: "blur" }
        ],
        commServerIp: [
           { max: 15, message: "通讯服务器ip地址不能超过15位", trigger: "blur" }
        ],
        commServerPort: [
           { type: "number", max: 2147483647, message: "通讯服务器端口地址不能超过2147483647", trigger: "blur", transform: (value) => Number(value) }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("term_kind").then(response => {
      this.termKindOptions = response.data;
    });
  },
  methods: {
    /** 查询参数管理列表 */
    getList() {
      this.loading = true;
      listPara(this.queryParams).then(response => {
        this.paraList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 终端类别字典翻译
    termKindFormat(row, column) {
      return this.selectDictLabel(this.termKindOptions, row.termKind);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        termImei: null,
        termKind: null,
        perBound: null,
        hstRecMaxLen: null,
        commServerId: null,
        commServerIp: null,
        commServerPort: null,
        gmtCreate: null,
        gmtModified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 设置参数按钮操作 */
    handleUpdate(row) {
      this.reset();
      const termImei = row.termImei || this.ids
      getPara(termImei).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '设置终端imei号为"'+termImei+'"的参数';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updatePara(this.form).then(response => {
            this.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    }
  }
};
</script>
